﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;


namespace LogicUniversity
{
    public partial class Approve : System.Web.UI.Page
    {
        ApproveControl ac;
        // Requisition re = new Requisition();

        CoreHeadFascade efc = new CoreHeadFascade();
        private EmailController mailControl;
        private MakeRequestControl requestControl;
        public Approve()
        {
            ac = new ApproveControl();
        }

        protected void Page_PreInit(Object sender, EventArgs e)
        {
            //if (Session["Role"] as string != "Employee" || Session["Role"] as string != "Representative")
            //{
            //    
            //}
            if (Session["Role"] as string != "Department Head")
            {
                if ((Session["TempRole"] as string).Trim() != "Department Head")
                {
                    Response.Redirect("~/Login.aspx");
                }
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                //GridView12.DataSource = ac.getAllRequisitions();
                //GridView12.DataBind();
                //Approved.Enabled = true;
                //Reject.Enabled = true;
                fillData();
            }


        }

        public void fillData()
        {
            string empID = Session["UserID"] as string;
            string deptCode = efc.getDepartmentCode(empID.Trim());
            List<EmployeeRequests> requests = efc.getEmployeeRequests(deptCode);

            if (requests == null || requests.Count <= 0)
            {
                lblMsg.Text = "Currently No Requests.";
                btnApprove.Visible = false;
                rejectBtn.Visible = false;
            }
            RequestsView.DataSource = requests;
            RequestsView.DataBind();
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach(GridViewRow row in RequestsView.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chkChoose");

                if (chk.Checked)
                {
                    HiddenField hf = (HiddenField)row.FindControl("reqID");
                    int reqID = Convert.ToInt32(hf.Value);

                    efc.updateApprove(reqID);

                    string employeeID =((LinkButton) row.Cells[1].Controls[1]).Text;
                    requestControl = new MakeRequestControl();
                    string deptCode = requestControl.getDepartmentCodeOf(employeeID);
                    mailControl = new EmailController();
                    string hostname = Dns.GetHostName();
                    IPAddress[] addressLst = Dns.GetHostAddresses(hostname);
                    string ipAdd = addressLst[1].ToString();
                    StringBuilder strb = new StringBuilder();
                    strb.Append("Stationery Request");
                    strb.Append("This is the message to approve the requestion make by " + requestControl.getEmpName(employeeID) + " . You can check from this link " + ipAdd + "/LogicUniversity/Department_Head/RequestsDetail.aspx?reqID=" + reqID + "click here!");
                    strb.Append("");
                    mailControl.send(requestControl.getFromEmail(employeeID), requestControl.getHeadEmail(deptCode), "Stationery Requisition", strb.ToString());

                    
                }

                
                i++;
            }
            lblMsg.Text = "Requisition has been approved";
            fillData();
        }

        protected void empID_Click(object sender, EventArgs e)
        {
            LinkButton btn = (LinkButton)sender;
            int reqID = Convert.ToInt32(btn.CommandArgument.Trim());
            string url = "RequestsDetail.aspx?ReqID=" + reqID;

            Response.Redirect(url);
        }

        protected void btnContinue_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (GridViewRow row in RequestsView.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chkChoose");

                if (chk.Checked)
                {
                    HiddenField hf = (HiddenField)row.FindControl("reqID");
                    int reqID = Convert.ToInt32(hf.Value.Trim());

                    efc.updateReject(reqID, txtReject.Text);
                   
                }

                
                i++;
                
            }
            fillData();
            lblMsg.Text = "Requisition has been rejected";
        }


    }

}















